package org.viettan.android.main.xml;

import java.io.IOException;

import org.viettan.android.main.io.UrlUtility;
import org.viettan.android.main.model.Article;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.google.inject.Inject;

/**
 * Handles parsing of {@link Article}-related XML.
 */
public class ArticleXmlParser extends BaseXmlParser<Article> {

    @Inject
    ArticleXmlParser() {
	super("article");
    }

    @Override
    protected void parseProperty(Article article, XmlPullParser parser)
	    throws XmlPullParserException, IOException {
	String tagName = parser.getName();
	if (tagName.equalsIgnoreCase("title")) {
	    article.setTitle(parser.nextText());
	} else if (tagName.equalsIgnoreCase("idart")) {
	    article.setId(Integer.parseInt(parser.nextText()));
	} else if (tagName.equalsIgnoreCase("idartsec")) {
	    article.setSectionId(Integer.parseInt(parser.nextText()));
	} else if (tagName.equalsIgnoreCase("intro")) {
	    article.setIntro(parser.nextText());
	} else if (tagName.equalsIgnoreCase("weblink")) {
	    article.setArticleWebUrl(UrlUtility.parseUrl(parser.nextText().trim()));
	} else if (tagName.equalsIgnoreCase("author")) {
	    article.setAuthor(parser.nextText());
	} else if (tagName.equalsIgnoreCase("bodytext")) {
	    article.setBody(parser.nextText());
	} else if (tagName.equalsIgnoreCase("media:thumbnail")
		|| tagName.equalsIgnoreCase("media:image")) {
	    article.setImageSmallUrl(UrlUtility.parseUrl(parser.getAttributeValue(null, "url")));
	} else if (tagName.equalsIgnoreCase("media:thumbnail2x")
		|| tagName.equalsIgnoreCase("media:image2x")) {
	    article.setImageLargeUrl(UrlUtility.parseUrl(parser.getAttributeValue(null, "url")));
	}
    }

    @Override
    protected Article createEntity(XmlPullParser parser) {
	return new Article();
    }
}
